package ru.spb.osll.myfit.wikiverifier;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map;

import org.junit.Test;

import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableStyle;
import ru.spb.osll.myfit.fixtures.centera.ClusterListFixture;
import ru.spb.osll.myfit.fixtures.centera.SetNodeRegenerationSubmitDelayFixture;
import ru.spb.osll.myfit.fixtures.centera.WithCubetoolsFixture;
import ru.spb.osll.myfit.server.parser.TableCreater;
import ru.spb.osll.myfit.server.parser.PageParserUtils;
import ru.spb.osll.myfit.util.CollectionUtils;
import ru.spb.osll.myfit.wikiverifier.result.PageVerificationResult;
import ru.spb.osll.myfit.wikiverifier.result.TableVerificationResult;
import ru.spb.osll.myfit.wikiverifier.result.VerificationResultType;

public class WikiVerifierTest {
	private WikiVerifier wikiVerifier;
	private TableCreater m_parser = new TableCreater();

	public WikiVerifierTest() {
		wikiVerifier = new WikiVerifier(getTemplates(), getFixtureClasses());
	}

	@SuppressWarnings("unchecked")
	public Map<String, Class<?>> getFixtureClasses() {
		return CollectionUtils.map(CollectionUtils.list(
				SetNodeRegenerationSubmitDelayFixture.class.getSimpleName(),
				WithCubetoolsFixture.class.getSimpleName(),
				ClusterListFixture.class.getSimpleName()), CollectionUtils
				.list((Class<?>) SetNodeRegenerationSubmitDelayFixture.class,
						WithCubetoolsFixture.class, ClusterListFixture.class));
	}

	public List<Table> getTemplates() {
		Table oneLineFixtreTemplate = m_parser.createTable(
				TableStyle.ONE_LINE_FIXTIRE,
				"set node regeneration submit delay", "30 min");
		Table doFixtureTemplate = m_parser.createTable(TableStyle.DO_FIXTURE,
				"with cubetools", TableCreater.LINE_SEPARATOR, "write", "10",
				"files of size", "10KB", "using", "10", "threads");
		Table setUpFixtureTemplate = m_parser.createTable(
				TableStyle.SETUP_FIXTURE, "cluster list",
				TableCreater.LINE_SEPARATOR, "name", "ip address",
				TableCreater.LINE_SEPARATOR, "cluster1", "1.2.3.4");

		return CollectionUtils.list(oneLineFixtreTemplate, doFixtureTemplate,
				setUpFixtureTemplate);
	}

	@Test
	public void testVerifyOk() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.ONE_LINE_FIXTIRE,
				"set node regeneration submit delay", "30 min"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyNotOk() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.ONE_LINE_FIXTIRE,
				"set nod regneration submit delay", "20 min"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageParserUtils.printSuggestion(tableResult, System.out);
				assertEquals("|set node regeneration submit delay|20 min|\n",
						PageParserUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyDoFixtureOk() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.DO_FIXTURE,
				"with cubetools", TableCreater.LINE_SEPARATOR, "write", "10",
				"files of size", "10KB", "using", "10", "threads"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifyDoFixtureNotOk() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.DO_FIXTURE,
				"with cubetools", TableCreater.LINE_SEPARATOR, "writ", "10",
				"files o size", "10KB", "using", "10", "threads"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageParserUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|with cubetools|\n|write|10|files of size|10KB|using|10|threads|\n",
						PageParserUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureOk() {
		Page page = new Page();
		page
				.insert(m_parser.createTable(TableStyle.SETUP_FIXTURE,
						"cluster list", TableCreater.LINE_SEPARATOR, "name",
						"ip address", TableCreater.LINE_SEPARATOR, "cluster1",
						"1.2.3.4"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		assertTrue(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureNotOk() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.SETUP_FIXTURE,
				"cluster list", TableCreater.LINE_SEPARATOR, "nam", "ip addr",
				TableCreater.LINE_SEPARATOR, "cluster1", "1.2.3.4"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageParserUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|cluster list|\n|name|ip address|\n|cluster1|1.2.3.4|\n",
						PageParserUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}

	@Test
	public void testVerifySetUpFixtureNotOk2() {
		Page page = new Page();
		page.insert(m_parser.createTable(TableStyle.SETUP_FIXTURE,
				"cluster list", TableCreater.LINE_SEPARATOR, "nam", "ap addr",
				TableCreater.LINE_SEPARATOR, "cluster1", "1.2.3.4"));
		PageVerificationResult res = wikiVerifier.verifyPage(page, true);
		for (TableVerificationResult tableResult : res.getTableResults()) {
			if (tableResult.getResult() != VerificationResultType.OK) {
				PageParserUtils.printSuggestion(tableResult, System.out);
				assertEquals(
						"|cluster list|\n|name|ip address|\n|cluster1|1.2.3.4|\n",
						PageParserUtils.tableToString(
								tableResult.getSuggestion()).replaceAll("\r",
								""));
			}
		}
		assertFalse(res.getResult() == VerificationResultType.OK);
	}
}
